package com.example.threaddemo.util;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author Boolean
 * @Date 2021/3/15 14:53
 * @Version 1.0
 *
 * 实现Callable接口通过FutureTask包装器来创建Thread线程
 */
public class RunnableDemo implements Runnable{
    private String name;

    public RunnableDemo(String name) {
        this.name=name;
    }
    public void run() {
        System.out.println("MyThread.run()");
    }

    public static void main(String[] args) {
/*
        for(int i=0;i<10;i++){
            //为了启动MyThread，需要首先实例化一个Thread，并传入自己的MyThread实例：
            RunnableDemo myThread = new RunnableDemo();

            Thread thread = new Thread(myThread);
            thread.start();
        }*/

        int xt =10;
        ExecutorService fixedThreadPool= Executors.newFixedThreadPool(xt);
        for(int i=0;i<xt;i++){
            //Thread  t =new ThreadDemo("t"+i);
            //fixedThreadPool.execute(t);
            fixedThreadPool.execute(new RunnableDemo("t"+i){
                public void run() {
                    // super.run();
                    try {
                        System.out.println("-----------总时间---------"+Thread.currentThread().getName());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
        fixedThreadPool.shutdown();


    }


}
